<?php
require_once APPLICATION_PATH.'\misc\sessionController.php';
require_once APPLICATION_PATH.'\misc\additional.php';
class FrontPageController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->disableLayout();
        $sessionController=new sessionController();
        $this->view->sessioncontroller = $sessionController;
        $this->view->transactions = new Application_Model_DbTable_Transaction();
        /* Initialize action controller here */
        
        // action body
        $sessionController = new sessionController();
        $cat_db = new Application_Model_DbTable_Category();
        $this->view-> categories = $cat_db->fetchAll();
        if($sessionController->checkSessionBuyer()){
            $this->view->contentcontroller='FrontPage';
            $this->view->contentaction='afterlogin';
            $sessionController->setBuyer();
            $this->view->buyerId=$sessionController->getSessionId();
            $this->view->username = $sessionController->getSessionUsername();
            $this->view->rightlabel = "Pembeli";
        }else{
            $this->view->contentcontroller='buyer';
            $this->view->contentaction='login';
            $this->view->buyerId=0;
            $this->view->rightlabel = "Login Pembeli";
        }
    }

    public function indexAction()
    {
        $this->_helper->layout->setLayout('frontlayout');
        
        $shop_db = new Application_Model_DbTable_Shop();
        $this->view->platinum = $shop_db->fetchAll("membership_id = 1 AND status = 1 AND deleted = 0");
    }

    public function getCategoryActionAction()
    {
        // action body
    }

    public function categoryAction()
    {
        $this->_helper->layout->setLayout('frontlayout');
        
        $id = $this->getRequest()->getParam('id', 0);

        if($id>0){
            $cat_db = new Application_Model_DbTable_Category();
            $shops = $cat_db->getShops($id);
            $this->view->shops = $shops;
        }
        
    }

    public function detailsAction()
    {
        $id = $this->getRequest()->getParam('id', 0);

        if($id>0){
            $shop_db = new Application_Model_DbTable_Shop();
            $shop_db->visited($id); // update hit counter
            $details = $shop_db->getShop($id);
            $this->view->details = $details;


        }
    
    }

    public function testimoniAction()
    {
        $this->_helper->layout()->disableLayout();
        $form = new Application_Form_Testimony();
        $form->submit->setLabel("Masukan Testimoni Anda");
        $this->view->form = $form;
        //$x = new Application_Model_DbTable_ShopKeyword();
        //echo count($x->assign());
        $testimony_id = $this->getRequest()->getParam('id', 0);
         
        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            //Validate input data
            if ($form->isValid($postData)) {         
                $testimony = new Application_Model_DbTable_Testimony();
                $testimony->insertTestimony($testimony_id, $form->getValue("Testimoni"));
            }

            //Input is not valid
            else {
                $form->populate($postData);
            }

       $this->_helper->redirector('list-testimony');
    }
    
  }


    public function listTestimonyAction()
    {
        // action body
         $this->_helper->layout()->disableLayout();

       
         $buyerid = new sessionController();
         $buyerid->setBuyer();
         $buyer_id = $buyerid->getSessionId();

        //get shop name
        $testimony_db = new Application_Model_DbTable_Testimony();
        $shops = $testimony_db->getTestimonybyBuyer($buyer_id);

        $this->view->shops = $shops;

        
    }



    public function afterloginAction()
    {   $this->_helper->layout->disableLayout();
        $sessionController = new sessionController();

        $sessionController->setBuyer();
        if($sessionController->checkSessionBuyer()){
        $transaction = new Application_Model_DbTable_Transaction();
        $buyer = new Application_Model_DbTable_Buyers();
        $total=$transaction->countRows("buyer_id=".$sessionController->getSessionId()
                                ." and transaction_status_id=1");

        $this->view->buyer=$buyer->fetchRow("buyer_id=".$sessionController->getSessionId());
        if($this->view->buyer['avatar']==null){
            $this->view->imageUser="/images/avatar/none.png";
        }else{
            $this->view->imageUser="/users/".$this->view->buyer['username']."/avatar/".$this->view->buyer['avatar'];
        }

        if($total==0){
            $this->view->image="trans.png";
            $this->view->theTitle="FRONT PAGE";
        }else{
            $this->view->theTitle="FRONT PAGE [$total]";
            $this->view->image="trans.png";
            $this->view->transAlert="$total transaksi yang belum terkonfirmasi";
        }
            $this->view->check=true;
        }else{
            $this->view->check=false;
        }
            

   }

    public function applyshopAction()
    {
        $this->_helper->layout->setLayout('frontlayout');
        
        $additional=new Additional();
        $form = new Application_Form_Shop();
        $form->submit->setLabel("Daftar");
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            //Validate input data
            if ($form->isValid($postData)) {
                $shops = new Application_Model_DbTable_Shop();
                $shops->addShop($form->getValue("name"),
                        $form->getValue("owner"),
                        $form->getValue("address"),
                        $form->getValue("city"),
                        $form->getValue("phone"),
                        $form->getValue("email"),
                        $form->getValue("url"),
                        $form->getValue("description"),
                        $form->getValue("item_sold"),
                        $form->getValue("membership")
                );

                // get id of new shop
                $shop_id = $shops->getId($form->getValue("membership"),
                                $form->getValue("email"), $form->getValue("phone"));


                // Assign categories
                $categories = $form->getValue("category");
                $shoptype = new Application_Model_DbTable_ShopType();
                $shoptype->assign($shop_id, $categories);

                // Assign Keywords
                $keywords = explode(",", $form->getValue("keyword"));
                $shopkey = new Application_Model_DbTable_ShopKeyword();
                $shopkey->assign($shop_id, $keywords);

                //$this->_helper->_flashMessenger->addMessage("Toko ".$form->getValue("name")." telah di daftarkan dan menunggu persetujuan");
                $this->_helper->redirector('applyshop');
            }

            //Input is not valid
            else {
                $form->populate($postData);
            }
        }
    }

    public function detailshopAction()
    {
        $this->_helper->layout->setLayout('frontlayout');
        
        $id = $this->getRequest()->getParam('id', 0);

        if($id>0){
            $shop_db = new Application_Model_DbTable_Shop();
            $shop_db->visited($id); // update hit counter
            $details = $shop_db->getShop($id);
            $this->view->details = $details;
            $sessionController=new sessionController();
            $sessionController->setBuyer();
          
            $this->view->buyerId = $sessionController->getSessionId();
            $this->view->image = "shop_".$id."_main.png";
            
            $testimony_db = new Application_Model_DbTable_Testimony();
            $this->view->testimonies = $testimony_db->getTestimonyByShop($id);
         }
    }

   
}













